package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntKeyInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntKeyInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/6
 */
public class MchntKeyInfoMapperImpl implements MchntKeyInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntKeyInfo(MchntKeyInfo mchntKeyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntKeyInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户密钥信息失败,instId:<{}>,mchntNo:<{}>,mchntAppId:<{}>,keyType:<{}>,iReturnCode:<{}>",
                    mchntKeyInfo.getInstId(), mchntKeyInfo.getMchntNo(), mchntKeyInfo.getMchntAppId(), mchntKeyInfo.getKeyType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntKeyInfoById(MchntKeyInfo mchntKeyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntKeyInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户密钥信息失败,instId:<{}>,mchntNo:<{}>,mchntAppId:<{}>,keyType:<{}>,iReturnCode:<{}>",
                    mchntKeyInfo.getInstId(), mchntKeyInfo.getMchntNo(), mchntKeyInfo.getMchntAppId(), mchntKeyInfo.getKeyType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntKeyInfo selectMchntKeyInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntKeyInfoById");
        MchntKeyInfo mchntKeyInfo = new MchntKeyInfo();
        mchntKeyInfo.setInstId(instId);
        mchntKeyInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, mchntKeyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户密钥信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntKeyInfo;
    }

    @Override
    public MchntKeyInfo selectMchntKeyInfoByPrimaryKey(String instId, String mchntNo, String mchntAppId, String keyType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntKeyInfoByPrimaryKey");
        MchntKeyInfo mchntKeyInfo = new MchntKeyInfo();
        mchntKeyInfo.setInstId(instId);
        mchntKeyInfo.setMchntNo(mchntNo);
        mchntKeyInfo.setMchntAppId(mchntAppId);
        mchntKeyInfo.setKeyType(keyType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, mchntKeyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户密钥信息失败,instId:<{}>,mchntNo:<{}>,mchntAppId:<{}>,keyType:<{}>,iReturnCode:<{}>", instId, mchntNo, mchntAppId, keyType, iReturnCode);
            return null;
        }
        return mchntKeyInfo;
    }
}
